package com.woniuxy.microsysserver.listener;

import com.alibaba.fastjson.JSONObject;
import com.woniuxy.microsysserver.bean.AdminInfo;
import com.woniuxy.microsysserver.mapper.OptMapper;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@RocketMQMessageListener(consumerGroup = "consumer_group",topic = "topic_txmsg")
public class TxmsgConsumer implements RocketMQListener<String> {

    @Resource
    OptMapper optMapper;

    /**
     * 导入消息推送对象
     */
    @Resource
    private SimpMessagingTemplate simpMessagingTemplate;

    @Override
    public void onMessage(String message) {
        System.out.println("start:"+message);
        //解析消息
        JSONObject jsonObject = JSONObject.parseObject(message);
        String no = jsonObject.getString("no");

        //幂等性处理
        if(optMapper.isConsumed(no) == 2){
            System.out.println("oooooooo");
        }else {
            //处理消息
            //我们使用这个方法进行消息的转发发送！
            String admin = jsonObject.getString("admin");
            AdminInfo adminInfo = JSONObject.parseObject(admin, AdminInfo.class);
            simpMessagingTemplate.convertAndSend("/topic/getLogin", adminInfo.getAdminName()+"下线了");
        };
    }
}
